package com.derbysoft.lowcostadapter.commons.log;

import com.derbysoft.lowcostadapter.commons.exception.SystemException;
import com.derbysoft.lowcostadapter.commons.model.ReservationLog;
import com.derbysoft.lowcostadapter.commons.model.SystemVariable;
import com.derbysoft.lowcostadapter.commons.model.TransactionLog;
import com.derbysoft.lowcostadapter.commons.redis.service.SystemVariableCacheService;
import com.derbysoft.lowcostadapter.commons.repository.ReservationLogRepository;
import com.derbysoft.lowcostadapter.commons.repository.SystemVariableRepository;
import com.derbysoft.lowcostadapter.commons.repository.TransactionLogRepository;
import com.derbysoft.lowcostadapter.dswitch2.service.helper.Constant;

import java.util.Set;
import java.util.concurrent.Executor;

/**
 * Created by: jason
 * Date: 2012-09-25
 */
public abstract class LogUtils {
    public static void saveLogs(Executor executor,
                                SystemVariableCacheService systemVariableCacheService,
                                SystemVariableRepository systemVariableRepository,
                                TransactionLogRepository transactionLogRepository,
                                ReservationLogRepository reservationLogRepository) {

        TransactionLog transactionLog = TransactionLogger.getAvailLogger().get();
        Set<LogDetailPair> logDetailPairs = TransactionLogger.getLogDetails().get();
        SystemVariable appendFailedOnly =
                loadSystemVariable(Constant.APPEND_FAILED_AVAIL_LOG_ONLY, systemVariableCacheService, systemVariableRepository);

        if (transactionLog != null) {
            boolean appendAllLogs = !Boolean.valueOf(appendFailedOnly.getValue());
            boolean appendFailedLogOnly = Boolean.valueOf(appendFailedOnly.getValue()) && transactionLog.isError();
            if (appendAllLogs || appendFailedLogOnly) {
                executor.execute(new TransactionLogTask(transactionLogRepository, transactionLog, logDetailPairs));
            }
            TransactionLogger.cleanAvailLogger();
        }
        ReservationLog reservationLog = TransactionLogger.getReservationLogger().get();
        if (reservationLog != null) {
            executor.execute(new ReservationLogTask(reservationLogRepository, reservationLog, logDetailPairs));
            TransactionLogger.cleanReservationLogger();
        }
        TransactionLogger.cleanLogDetails();
    }

    private static SystemVariable loadSystemVariable(String name,
                                                     SystemVariableCacheService systemVariableCacheService,
                                                     SystemVariableRepository systemVariableRepository) {
        SystemVariable systemVariable = systemVariableCacheService.loadByName(name);
        if (systemVariable == null) {
            systemVariable = systemVariableRepository.findByName(name);
        }
        if (systemVariable == null) {
            throw new SystemException(String.format("system config not found:[%s] !!", name));
        }
        return systemVariable;
    }
}
